import request from '@/utils/request'
import { randomId } from '@/utils'
import memCache from '@/utils/memCache'
// /product/getById/{id}
// 根据id获取详情
export function getById(id) {
  return request({
    method: 'GET',
    url: `/app/v1/product/getById/${id}`
  })
}
// /product/getMadeAttribute
// 获取搜索条件-定制
export function getMadeAttributeFun() {
  return request({
    method: 'GET',
    url: `/app/v1/product/getMadeAttribute`
  }).then(res => {
    const result = []
    // spaceList 空间
    result.push({
      title: '空间',
      flag: 'spaceList',
      unId: randomId(),
      multiple: true,
      children: (res.data.spaceList || []).map((e, i) => {
        return {
          label: e.value,
          value: e.key,
          index: i
        }
      })
    })
    result.push({
      title: '风格',
      flag: 'styleList',
      unId: randomId(),
      children: (res.data.styleList || []).map((e, i) => {
        e.label = e.styleName
        e.value = e.styleCode
        e.index = i
        // 系列
        e.title = `系列`
        e.unId = randomId()
        e.flag = `seriesList`
        e.children = (e.seriesList || []).map((c, i) => {
          c.label = c.seriesName
          c.value = c.seriesCode
          c.index = i
          return c
        })
        return e
      })
    })
    return {
      rawDdata: res.data,
      data: result
    }
  })
}
export const getMadeAttribute = memCache('getMadeAttribute', getMadeAttributeFun)
// /product/getAllMadeAttribute
// 获取搜索条件-定制 搜索页面
export function getAllMadeAttributeFun() {
  return request({
    method: 'GET',
    url: `/app/v1/product/getAllMadeAttribute`
  }).then(res => {
    const result = []
    // spaceList 空间
    result.push({
      title: '空间',
      flag: 'spaceList',
      unId: randomId(),
      multiple: true,
      children: (res.data.spaceList || []).map((e, i) => {
        return {
          label: e.value,
          value: e.key,
          index: i
        }
      })
    })
    result.push({
      title: '风格',
      flag: 'styleList',
      unId: randomId(),
      children: (res.data.styleList || []).map((e, i) => {
        e.label = e.styleName
        e.value = e.styleCode
        e.index = i
        // 系列
        e.title = `系列`
        e.unId = randomId()
        e.flag = `seriesList`
        e.children = (e.seriesList || []).map((c, i) => {
          c.label = c.seriesName
          c.value = c.seriesCode
          c.index = i
          return c
        })
        return e
      })
    })
    return {
      rawDdata: res.data,
      data: result
    }
  })
}
export const getAllMadeAttribute = memCache('getAllMadeAttribute', getAllMadeAttributeFun)
// /product/getMadePage
// 分页获取-产品定制
export function getMadePage(data) {
  return request({
    method: 'POST',
    url: `/app/v1/product/getMadePage`,
    data,
    loading: false
  })
}
// /product/getProductAttribute
// 获取搜索条件-橱柜
export function getProductAttributeFun() {
  return request({
    method: 'GET',
    url: `/app/v1/product/getProductAttribute`
  }).then(res => {
    const result = []
    // spaceList 空间
    result.push({
      title: '空间',
      flag: 'spaceList',
      unId: randomId(),
      multiple: true,
      children: (res.data.spaceList || []).map((e, i) => {
        return {
          label: e.value,
          value: e.key,
          index: i
        }
      })
    })
    result.push({
      title: '风格',
      flag: 'styleList',
      unId: randomId(),
      children: (res.data.styleList || []).map((e, i) => {
        e.label = e.styleName
        e.value = e.styleCode
        e.index = i
        // 系列
        e.title = `系列`
        e.unId = randomId()
        e.flag = `seriesList`
        e.children = (e.seriesList || []).map((c, i) => {
          c.label = c.seriesName
          c.value = c.seriesCode
          c.index = i
          return c
        })
        return e
      })
    })
    return {
      rawDdata: res.data,
      data: result
    }
  })
}
export const getProductAttribute = memCache('getProductAttribute', getProductAttributeFun)
// /product/getProductPage
// 分页获取-产品橱柜
export function getProductPage(data) {
  return request({
    method: 'POST',
    url: `/app/v1/product/getProductPage`,
    data,
    loading: false
  })
}
